package xanatorium.Zeiterfassung.data;

import java.util.Calendar;

public class Entry
{
	public Calendar startTime = null;
	public Calendar endTime = null;
	int breakminutes = 0;
	String description = "";
	String key = "IW";
	
	public Entry()
	{
		start();
	}
	
	public Calendar getStartTime()
	{
		return startTime;
	}
	
	public void setStartTime(Calendar startTime)
	{
		this.startTime = startTime;
	}
	
	public void setStartHour(int hour)
	{
		startTime.set(Calendar.HOUR_OF_DAY, hour);
	}
	
	public void setStartMinute(int minute)
	{
		startTime.set(Calendar.MINUTE, minute);
	}
	
	public void setEndHour(int hour)
	{
		endTime.set(Calendar.HOUR_OF_DAY, hour);
	}
	
	public void setEndMinute(int minute)
	{
		endTime.set(Calendar.MINUTE, minute);
	}
	
	public Calendar getEndTime()
	{
		return endTime;
	}
	
	public boolean isStarted()
	{
		return startTime != null;
	}
	
	public boolean isEnded()
	{
		return endTime != null;
	}
	
	public void end()
	{
		endTime = Calendar.getInstance();
	}
	
	public void start()
	{
		startTime = Calendar.getInstance();
	}
	
	public void setEndTime(Calendar endTime)
	{
		this.endTime = endTime;
	}
	
	public void setBreakminutes(int breakminutes)
	{
		this.breakminutes = breakminutes;
	}
	
	public int getBreakminutes()
	{
		return breakminutes;
	}
	
	long getHoursDifference()
	{
		
		long milis1 = startTime.getTimeInMillis();
		
		long milis2 = endTime.getTimeInMillis();
		
		long diff = milis2 - milis1;
		
		long diffHours = diff / (60 * 60 * 1000);
		
		return diffHours;
		
	}
	
	long getMinutesDifference()
	{
		
		long milis1 = startTime.getTimeInMillis();
		
		long milis2 = endTime.getTimeInMillis();
		
		long diff = milis2 - milis1;
		long diffhours = getHoursDifference() * 1000;
		long diffMinutes = (diff - diffhours) / (60 * 1000);
		return diffMinutes;
	}
	
	long getgetMinutesDifferenceForString()
	{
		long minutes = getMinutesDifference();
		return minutes <= 0 ? minutes : 60 - minutes;
	}
	
	String breaksSubtracted()
	{
		long minutes = getMinutesDifference() - breakminutes;
		long hours = getHoursDifference();
		if (minutes >= 0 || hours == 0)
		{
			return (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 && minutes > 0 ? "0" : "") + minutes;
		}
		else
		{
			hours -= 1;
			long min = 60 - minutes;
			return (hours < 10 ? "0" : "") + hours + ":" + (min < 10 && min > 0 ? "0" : "") + min;
		}
		
	}
	
	@Override
	public String toString()
	{
		
		return (startTime.get(Calendar.DAY_OF_MONTH) + ".") + (startTime.get(Calendar.MONTH) + 1) + " | "
				+ (startTime.get(Calendar.HOUR_OF_DAY) < 10 ? "0" : "") + startTime.get(Calendar.HOUR_OF_DAY) + ":"
				+ (startTime.get(Calendar.MINUTE) < 10 ? "0" : "") + startTime.get(Calendar.MINUTE) + " - "
				+ (endTime.get(Calendar.HOUR_OF_DAY) < 10 ? "0" : "") + endTime.get(Calendar.HOUR_OF_DAY) + ":"
				+ (endTime.get(Calendar.MINUTE) < 10 ? "0" : "") + endTime.get(Calendar.MINUTE) + " " + breakminutes
				+ "=" + breaksSubtracted();
	}
}
